package com.enniswu.weitter.moment.posting;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.moment.postlist.PostListPresenter;
import com.enniswu.weitter.network.HttpClient;

import java.text.SimpleDateFormat;
import java.util.Date;

public class PostingPresenter extends PostListPresenter<PostingContract.View> implements PostingContract.Presenter {

    public PostingPresenter(PostingContract.View view, int type, int orientation) {
        super(view, type, orientation);
    }

    @Override
    public void comment(final String contend) {
        if (TextUtils.isEmpty(contend)) {
            view.next(null);
            return;
        }
        pLoader.post(contend, type, orientation, new AbstractCallback<Integer>() {
            @Override
            public void onSuccess(@Nullable Integer integer) {
                if (view == null)
                    return;
                if (integer == null) {
                    view.showMessage(HttpClient.getErrorMessage());
                    return;
                }
                Post post = new Post();
                post.setId(integer);
                post.setType(type);
                post.setTargetId(orientation);
                post.setUserId(SPSupport.getInstance().getId());
                post.setCreateTime(SimpleDateFormat.getDateTimeInstance().format(new Date()));
                post.setContext(contend);
                view.next(post);
            }
        });
    }
}
